package com.fw.service.persistence.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;

import com.fw.service.persistence.MerchantUserPersistenceService;
import com.fw.service.persistence.dao.MerchantUserDAO;
import com.fw.service.persistence.dto.MerchantUser;

public class MerchantUserPersistenceServiceImpl implements MerchantUserPersistenceService {

    @Autowired
    private MerchantUserDAO merchantUserDAO;

    @Override
    public List<MerchantUser> selectByUsernameAndStatus(final String username, final String status) {

	Specification<MerchantUser> specification = new Specification<MerchantUser>() {
	    @Override
	    public Predicate toPredicate(Root<MerchantUser> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		List<Predicate> predicates = new ArrayList<Predicate>();
		if (username != null && !username.isEmpty()) {
		    predicates.add(cb.like(root.<String> get("username"), username));
		}
		if (status != null && !status.isEmpty()) {
		    predicates.add(cb.equal(root.<String> get("status"), status));
		}
		Predicate result = cb.conjunction();
		for (Predicate predicate : predicates) {
		    result.getExpressions().add(predicate);
		}
		return result;
	    }
	};
	return merchantUserDAO.findAll(specification);
    }

    @Override
    public List<MerchantUser> selectByStatus(final String status) {
	Specification<MerchantUser> specification = new Specification<MerchantUser>() {
	    @Override
	    public Predicate toPredicate(Root<MerchantUser> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		List<Predicate> predicates = new ArrayList<Predicate>();
		if (status != null && !status.isEmpty()) {
		    predicates.add(cb.equal(root.<String> get("status"), status));
		}
		Predicate result = cb.conjunction();
		for (Predicate predicate : predicates) {
		    result.getExpressions().add(predicate);
		}
		return result;
	    }
	};
	return merchantUserDAO.findAll(specification);
    }

    @Override
    public MerchantUser save(MerchantUser merchantUser) {
	return merchantUser == null ? null : merchantUserDAO.save(merchantUser);
    }

    @Override
    public MerchantUser findOne(Long id) {
	return id == null ? null : merchantUserDAO.findOne(id);
    }

    @Override
    public Page<MerchantUser> findAll(Specification<MerchantUser> specification, PageRequest pageRequest) {
	return specification == null || pageRequest == null ? null : merchantUserDAO.findAll(specification, pageRequest);
    }

    @Override
    public List<Object> selectDistinctLastUpdateBy() {
	return merchantUserDAO.selectDistinctLastUpdateBy();
    }

    @Override
    public List<Object> selectDistinctCreateBy() {
	return merchantUserDAO.selectDistinctCreateBy();
    }

    @Override
    public List<Object> selectDistinctEmail() {
	return merchantUserDAO.selectDistinctEmail();
    }
}
